<template>
  <component :is="linkType" v-bind="linkProps(to)">
    <slot />
  </component>
</template>

<script lang="ts" setup>
import { isExternal } from '@/utils'

defineOptions({
  name: 'AppLink',
  inheritAttrs: false // 禁止默认的透传行为，默认值：true
})

const props = defineProps<{ to: string }>()

const isExternalLink = computed(() => {
  return isExternal(props.to || '')
})

const linkType = computed(() => (isExternalLink.value ? 'a' : 'router-link'))

const linkProps = (to: string) => {
  if (isExternalLink.value) {
    return {
      href: to,
      target: '_blank',
      rel: 'noopener noreferrer'
    }
  }
  return { to }
}
</script>
<style lang="scss" scoped></style>
